package fr.imag.ufrima.ctte.gui.menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuBar;

import fr.imag.ufrima.ctte.ImagCTTE;
import fr.imag.ufrima.ctte.TaskDiagram;
import fr.imag.ufrima.ctte.events.ImagCtteAdapter;
import fr.imag.ufrima.ctte.events.ImagCtteEvent;

public class MainMenuBar extends JMenuBar {
	private ImagCTTE imagCTTE;
	/**
	 * Constructor
	 */
	public MainMenuBar(ImagCTTE imagCTTE){
		this.imagCTTE = imagCTTE;
		InitComponents();
		imagCTTE.addImagCtteListener(new ImagCtteAdapter(){			
			public void taskDiagramSwitched(ImagCtteEvent event) {
				super.taskDiagramSwitched(event);
				TaskDiagram taskDiagram = event.getTaskDiagram();
				if (taskDiagram == null){
					closeMenuItem.setEnabled(false);
					saveMenuItem.setEnabled(false);
					saveAsImageMenuItem.setEnabled(false);
					printMenuItem.setEnabled(false);					
					findMenuItem.setEnabled(false);
					
				} else {
					closeMenuItem.setEnabled(true);
					saveMenuItem.setEnabled(true);
					saveAsImageMenuItem.setEnabled(true);					
					printMenuItem.setEnabled(true);					
					findMenuItem.setEnabled(true);
				}
			}
		});
	}
	/**
	 * Initialize components
	 */
	protected void InitComponents(){
		fileMenu = new javax.swing.JMenu("File");		
        editMenu = new javax.swing.JMenu("Edit");
        infoMenu = new javax.swing.JMenu("Statistics");
        helpMenu = new javax.swing.JMenu("Help");
        
        newMenuItem = new javax.swing.JMenuItem("New");
        newMenuItem.addActionListener(new ActionListener(){        	
        	public void actionPerformed(ActionEvent arg0) {
        		imagCTTE.newTaskDiagram();
        	}
        });
        openMenuItem = new javax.swing.JMenuItem("Open");
        closeMenuItem = new javax.swing.JMenuItem("Close");
        closeMenuItem.setEnabled(false);
        closeMenuItem.addActionListener(new ActionListener(){        	
        	public void actionPerformed(ActionEvent arg0) {
        		imagCTTE.closeCurrentTaskDiagram();
        	}
        });
        closeAllMenuItem = new javax.swing.JMenuItem("Close All");
        closeAllMenuItem.setEnabled(false);
        closeAllMenuItem.addActionListener(new ActionListener(){        	
        	public void actionPerformed(ActionEvent arg0) {
        		imagCTTE.closeAllTaskDiagrams();
        	}
        });
        saveMenuItem = new javax.swing.JMenuItem("Save");
        saveMenuItem.setEnabled(false);
        saveAsImageMenuItem = new javax.swing.JMenuItem("Save As Image");
        saveAsImageMenuItem.setEnabled(false);
        printMenuItem =new  javax.swing.JMenuItem("Print");
        printMenuItem.setEnabled(false);
        quitMenuItem = new javax.swing.JMenuItem("Quit");
        quitMenuItem.addActionListener(new ActionListener(){        	
        	public void actionPerformed(ActionEvent arg0) {
        		imagCTTE.quitApplication();
        	}
        });
        undoMenuItem = new javax.swing.JMenuItem("Undo");
        undoMenuItem.setEnabled(false);
        redoMenuItem = new javax.swing.JMenuItem("Redo");
        redoMenuItem.setEnabled(false);
        cutMenuItem = new javax.swing.JMenuItem("Cut");
        cutMenuItem.setEnabled(false);
        cutSubTreeMenuItem = new javax.swing.JMenuItem("Cut SubTree");
        cutSubTreeMenuItem.setEnabled(false);
        copyMenuItem = new javax.swing.JMenuItem("Copy");
        copyMenuItem.setEnabled(false);
        copySubTreeMenuItem = new javax.swing.JMenuItem("Copy SubTree");
        copySubTreeMenuItem.setEnabled(false);
        pastMenuItem = new javax.swing.JMenuItem("Past");
        pastMenuItem.setEnabled(false);
        deleteMenuItem = new javax.swing.JMenuItem("Delete");
        deleteMenuItem.setEnabled(false);
        findMenuItem = new javax.swing.JMenuItem("Find");
        findMenuItem.setEnabled(false);
        taskModelStatisticsMenuItem = new javax.swing.JMenuItem("Task Model Statistics");
        taskModelStatisticsMenuItem.setEnabled(false);
        aboutMenuItem = new javax.swing.JMenuItem("About ImagCTTE");
        
     // menu 		
		fileMenu.add(newMenuItem);			
		fileMenu.add(openMenuItem);
		fileMenu.addSeparator();
		fileMenu.add(closeMenuItem);
		fileMenu.add(closeAllMenuItem);
		fileMenu.addSeparator();	
		fileMenu.add(saveMenuItem);
		fileMenu.add(saveAsImageMenuItem);
		fileMenu.addSeparator();
		fileMenu.add(printMenuItem);        
		fileMenu.addSeparator();
		fileMenu.add(quitMenuItem);		
		this.add(fileMenu);
		
		editMenu.add(undoMenuItem);
		editMenu.add(redoMenuItem);
		editMenu.addSeparator();	
		editMenu.add(cutMenuItem);
		editMenu.add(cutSubTreeMenuItem);
		editMenu.add(copyMenuItem);
		editMenu.add(copySubTreeMenuItem);
		editMenu.add(pastMenuItem);
		editMenu.add(deleteMenuItem);
		editMenu.addSeparator();
		editMenu.add(findMenuItem);
		this.add(editMenu);
        
		infoMenu.add(taskModelStatisticsMenuItem);
		this.add(infoMenu);
		
		helpMenu.add(aboutMenuItem);
		this.add(helpMenu);
	}
	/**
	 * Components
	 */
	private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenu infoMenu;
    private javax.swing.JMenu helpMenu;
    
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem closeMenuItem;
    private javax.swing.JMenuItem closeAllMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JMenuItem saveAsImageMenuItem;
    private javax.swing.JMenuItem printMenuItem;
    private javax.swing.JMenuItem quitMenuItem;
    private javax.swing.JMenuItem undoMenuItem;
    private javax.swing.JMenuItem redoMenuItem;
    private javax.swing.JMenuItem cutMenuItem;
    private javax.swing.JMenuItem cutSubTreeMenuItem;
    private javax.swing.JMenuItem copyMenuItem;
    private javax.swing.JMenuItem copySubTreeMenuItem;
    private javax.swing.JMenuItem pastMenuItem;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JMenuItem findMenuItem;
    private javax.swing.JMenuItem taskModelStatisticsMenuItem;
    private javax.swing.JMenuItem aboutMenuItem;   
}
